/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package classes;

import javax.servlet.http.HttpServletRequest;

/**
 * This class generates a url and can return it history; Especially for when you
 * login; Then you want to be redirected to the same page again.
 *
 * @author Sander
 */
public class Url {

    private static String history = "";

    public Url() {
    }

    /**
     * This method generates a url from the exact location where you are on the
     * site and returns this url.
     *
     * @param request
     * @return
     */
    public static String getUrl(HttpServletRequest request) {
        String url = "";
        String rememberpath = "";
        String servlet_path = (String) request.getAttribute("javax.servlet.forward.servlet_path");
        String query_string = (String) request.getAttribute("javax.servlet.forward.query_string");

        if (servlet_path != null) {
            rememberpath += servlet_path;
            url += servlet_path;
        } else {
            url += request.getServletPath();
        }
        if (query_string != null) { // If the url has get parameters
            url += "?" + request.getAttribute("javax.servlet.forward.query_string");
        }
        if (url.substring(0, 1).equals("/")) {
            url = url.substring(1, url.length());
        }
        if (url.contains("jsp/index.jsp")) {
            url = rememberpath;
            return url;
        } else {
            return url;
        }

    }

    public static String getHistory() {
        return history;
    }

    public static void setHistory(HttpServletRequest request) {
        history = getUrl(request);
    }
}
